package com.woniu.defecttracking.controller.bugs;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.defecttracking.commens.ComUtil;
import com.woniu.defecttracking.commens.Result;
import com.woniu.defecttracking.dto.AddFeedBackDto;
import com.woniu.defecttracking.entity.BugInfo;
import com.woniu.defecttracking.entity.FeedBack;
import com.woniu.defecttracking.exception.FeedBackException;
import com.woniu.defecttracking.param.AddFeedBackParam;
import com.woniu.defecttracking.service.impl.BugInfoServiceImpl;
import com.woniu.defecttracking.service.impl.FeedBackServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 *
 * @author 李东辉
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/feedBack")
public class FeedBackController {
    /*
    *
    * 增 根据bugid  增内容 时间 编号
    * 如果bugid查的status为新建、关闭 则报错
    *
    * 查 根据bugid 返回编号 内容 时间
    * */

    @Autowired
    private FeedBackServiceImpl feedBackService;
    @Autowired
    private BugInfoServiceImpl bugInfoService;

    /**
    * @作者: lidonghui
    * @描述：新增一条反馈记录
    * @时间：2022/1/1 9:52
    * @参数：[addFeedBackParam]
    * @参数类型: AddFeedBackParam
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("/addFeedBack")
    @ResponseBody
    public Result addFeedBack(@RequestBody AddFeedBackParam addFeedBackParam) throws FeedBackException {

        Result result = new Result();
//        try{
            AddFeedBackDto addFeedBackDto = ComUtil.copyProperties(addFeedBackParam, AddFeedBackDto.class);
            // AddFeedBackDto addFeedBackDto1 = feedBackService.addFeedBack(addFeedBackDto);

            //判断这条bug是否是新建或者已关闭状态，如果是。则不能进行反馈
            //0-新建 1-进行中 2-已解决 3-已关闭
//            BugInfo bugInfo = bugInfoService.getBaseMapper().selectByBugId(addFeedBackParam.getBugId());

            if(addFeedBackParam.getStatues(). equals("0")){
                throw new FeedBackException("该缺陷已关闭或者新建状态，不可提交反馈",1031);
            }

            String time = ComUtil.nowDateTime();
            //生成反馈编号
            String code = ComUtil.emailCode(5);

            FeedBack feedBack = ComUtil.copyProperties(addFeedBackDto, FeedBack.class);
            feedBack.setFeedBackNum(code);
            feedBack.setFeedBackTime(time);
            int insert = feedBackService.getBaseMapper() .insert(feedBack);
            if(0<insert){
                result.setCode(200);
                result.setMessage("新增成功");
                result.setData(ComUtil.copyProperties(feedBack,AddFeedBackDto.class));
            }


//        }catch (FeedBackException fe){
//            throw new FeedBackException(fe.getMessage(),1031);
//        }catch (Exception e){
//            throw new FeedBackException("新增反馈记录失败",1031);
//        }

        return result;
    }

    /**
    * @作者: lidonghui
    * @描述：根据反馈id删除反馈记录
    * @时间：2022/1/1 11:36
    * @参数：[feedBackId]
    * @参数类型:
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @ResponseBody
    @GetMapping("/deleteFeedBackById/{feedBackId}")
    public Result deleteFeedBackById(@PathVariable("feedBackId") Integer feedBackId) throws FeedBackException {
        Result result = new Result();
        try{
            //先查询该记录是否存在
             this.getFeedbackById(feedBackId);

            int i = feedBackService.getBaseMapper().updateIsDel(feedBackId);
            if(0<i){
                result.setCode(200);
                result.setMessage("删除成功");
            }
        }catch (FeedBackException e){
            throw new FeedBackException(e.getMessage(),1032);
        }
        catch (Exception e){
            throw new FeedBackException("逻辑删除反馈记录失败",1032);
        }


        return result;
    }
    /**
    * @作者: lidonghui
    * @描述：根据反馈id查询结果
    * @时间：2022/1/1 17:13
    * @参数：[feedBackId]
    * @参数类型:
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @GetMapping("/getFeedbackById/{feedBackId}")
    public Result getFeedbackById(@PathVariable("feedBackId") Integer feedBackId) throws FeedBackException {
        Result result = new Result();
        try{
            FeedBack feedBack = feedBackService.getBaseMapper().selectById(feedBackId);
            if(null!=feedBack){
                result.setCode(200);
                result.setMessage("查询成功");
                result.setData(feedBack);
            }else {
                throw new FeedBackException("该反馈记录不存在",1030);
            }
        }catch(FeedBackException e){
            throw new FeedBackException(e.getMessage(),1030);
        }
        catch(Exception e){
            throw new FeedBackException("获取反馈记录失败",1030);
        }



        return result;
    }

    /**
    * @作者: lidonghui
    * @描述：根据bugid查询反馈结果
    * @时间：2022/1/1 17:13
    * @参数：
    * @参数类型:
    * @返回：
    **/
    @GetMapping("/getFeedbackListByBugId/{bugId}")
    public Result getFeedbackListByBugId(@PathVariable("bugId") Integer bugId) throws FeedBackException {
        Result result = new Result();
        System.out.println("bugId=========="+bugId);
//        try{
            List<FeedBack> list = feedBackService.getBaseMapper().selectList(new QueryWrapper<FeedBack>().eq("bug_Id", bugId));

            if(list.size()>0){
                result.setCode(200);
                result.setMessage("查询成功");
                result.setData(list);
            }else {
                throw new FeedBackException("该反馈记录不存在",1030);
            }
//        }catch (FeedBackException e){
//            throw new FeedBackException(e.getMessage(),1030);
//        }
//        catch (Exception e){
//            throw new FeedBackException("获取反馈记录失败",1030);
//        }


        return result;
    }
}

